package thread.threaddemo;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: wenjingyuan
 * @Date: 2022/11/01/21:01
 * @Description:实现callable  新建线程
 */
public class ThreadDemo8 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable=new MyCallable();
        FutureTask<Integer> futureTask=new FutureTask<>(myCallable);
        Thread thread=new Thread(futureTask);
        //启动线程
        thread.start();
        //拿到结果
        int result=futureTask.get();
        System.out.println(Thread.currentThread().getName());
        System.out.println("随机数为"+result);
    }
}
class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        //具体的业务代码
        //随机数0-9
        int random=new Random().nextInt(10);
        System.out.println(Thread.currentThread().getName()+"随机数为"+random);
        return random;
    }
}